<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>PDF预览</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.11.174/pdf.min.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body {
            background: #525659;
        }
        #viewerContainer {
            width: 100%;
            height: 100vh;
            overflow: auto;
            background: #525659;
            position: relative;
        }
        #viewer {
            width: 100%;
            height: 100%;
            display: flex;
            flex-direction: column;
            align-items: center;
            padding: 10px;
        }
        .page {
            margin-bottom: 10px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.2);
        }
        canvas {
            max-width: 100%;
            height: auto !important;
            background: white;
        }
    </style>
</head>
<body>
    <div id="viewerContainer">
        <div id="viewer"></div>
    </div>

    <script>
        // 配置 PDF.js worker
        pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.11.174/pdf.worker.min.js';

        // 获取URL参数
        const urlParams = new URLSearchParams(window.location.search);
        const pdfUrl = decodeURIComponent(urlParams.get('file'));

        // 加载并渲染PDF
        async function loadPDF() {
            try {
                const loadingTask = pdfjsLib.getDocument(pdfUrl);
                const pdf = await loadingTask.promise;
                const viewer = document.getElementById('viewer');

                // 渲染所有页面
                for (let pageNum = 1; pageNum <= pdf.numPages; pageNum++) {
                    const page = await pdf.getPage(pageNum);
                    const viewport = page.getViewport({ scale: 1.5 });

                    const pageContainer = document.createElement('div');
                    pageContainer.className = 'page';
                    viewer.appendChild(pageContainer);

                    const canvas = document.createElement('canvas');
                    pageContainer.appendChild(canvas);

                    const context = canvas.getContext('2d');
                    canvas.height = viewport.height;
                    canvas.width = viewport.width;

                    await page.render({
                        canvasContext: context,
                        viewport: viewport
                    }).promise;
                }
            } catch (error) {
                console.error('Error loading PDF:', error);
                alert('PDF加载失败');
            }
        }

        loadPDF();
    </script>
</body>
</html> 